package com.pingan.myBatis.crypt.handler;

import com.pingan.myBatis.crypt.utils.DESUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;

/**
 * @AUTHOR:LIUCHAO;
 * @DATE: 2020/11/30 16:30
 */
public class StringHandler {

    /**
     * String 加密
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String stringEncrypt(String str) throws Exception {
        return stringEncrypt(null, str, null, null);
    }

    /**
     * String 加密
     *
     * @param str
     * @param set
     * @return
     * @throws Exception
     */
    public static String stringEncrypt(String str, Set<String> set) throws Exception {
        return stringEncrypt(null, str, set, true);
    }

    /**
     * String 加密
     *
     * @param name
     * @param str
     * @param set
     * @return
     * @throws Exception
     */
    public static String stringEncrypt(String name, String str, Set<String> set) throws Exception {
        return stringEncrypt(name, str, set, false);
    }

    /**
     * String 加密
     *
     * @param name
     * @param str
     * @param set
     * @param isSingle
     * @return
     * @throws Exception
     */
    public static String stringEncrypt(String name, String str, Set<String> set, Boolean isSingle) throws Exception {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        if (isSingle == null) {
            //todo 加密实现
             str = DESUtil.getEncryptString(str);
//            str = "";
            return str;
        }
        if (isSingle && set != null && !set.isEmpty()) {
            //todo 加密实现
            str = DESUtil.getEncryptString(str);
            return str;
        }
        if (!isSingle && set != null && !set.isEmpty() && set.contains(name)) {
            //todo 加密实现
            str = DESUtil.getEncryptString(str);
            return str;
        }

        return str;
    }

    /**
     * String 解密
     *
     * @param str
     * @return
     */
    public static String stringDecrypt(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        //todo 解密实现
        str = DESUtil.getDecryptString(str);

        return str;
    }


}
